package com.ayou.eggguard.farm.controller;

import com.ayou.common.utils.Result;
import com.ayou.eggguard.farm.pojo.ChickenCoop;
import com.ayou.eggguard.farm.pojo.FeedRecord;
import com.ayou.eggguard.farm.service.ChickenCoopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/chickenCoop")
public class ChickenCoopController {
    @Autowired
    private ChickenCoopService chickenCoopService;


    //为指定鸡场添加鸡舍
    @PostMapping("/addCoop")
    public Result addCoop(@RequestParam String chickenFarmName,@RequestBody ChickenCoop chickenCoop){

        ChickenCoop coop=chickenCoopService.getCoopByNumber(chickenCoop.getCoopNumber());
        if(coop==null){
            chickenCoopService.addCoop(chickenFarmName,chickenCoop);
            return Result.success("鸡舍添加成功");
        }else{
            return Result.error("鸡舍编号重复");
        }
    }

    //根据鸡舍编号修改鸡舍信息
    @PutMapping("/updateCoopInfo")
    public Result updateCoopInfo(@RequestBody ChickenCoop chickenCoop) {

        ChickenCoop coop = chickenCoopService.getCoopByNumber(chickenCoop.getCoopNumber());
        if (coop == null) {
            return Result.error("鸡舍编号不存在");
        }
        chickenCoopService.updateCoopInfo(chickenCoop);
        return Result.success("鸡舍信息修改成功");

    }

    //查询某一鸡场下的全部鸡舍
    @GetMapping("/getCoops")
    public Result<List<ChickenCoop>> getCoopsByFarmName(String farmName){
        List<ChickenCoop> coops=chickenCoopService.getCoopsByFarmName(farmName);
        return Result.success(coops);
    }

    //删除鸡舍
    @GetMapping("/deleteCoop")
    public Result deleteCoop(String coopNumber){
        chickenCoopService.deleteCoop(coopNumber);
        return Result.success();
    }

    //获取鸡舍喂养信息
    @GetMapping("/getRecords")
    public Result<List<FeedRecord>> getFeedRecord(String coopNumber){
        List<FeedRecord> records=chickenCoopService.getFeedRecord(coopNumber);
        return Result.success(records);
    }
}
